package com.jwaoo.mobile.view.forgotpassword;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.dx.as.LoadingDialog;
import com.dx.as.ToolBarOptions;
import com.dx.as.UI;
import com.dx.as.common.CommonUtils;
import com.dx.as.common.JWConstant;
import com.dx.as.utils.AESSecurityUtil;
import com.jwaoo.mobile.R;
import com.jwaoo.mobile.view.forgotpassword.presenter.PasswordPresenter;

public class ForgotPasswordActivity extends UI implements IPasswordView{

    private EditText edt_email;
    private TextInputLayout lb_email;
    private Button btn_send_email;
    private PasswordPresenter presenter;
    public final static int REQUEST_CODE = 1001;
    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login_send_email);
        initView();
        presenter = new PasswordPresenter(this,this);
    }

    private void initView(){
        edt_email = findView(R.id.edt_email);
        lb_email = findView(R.id.lb_email);
        btn_send_email = findView(R.id.btn_send_email);
        edt_email.addTextChangedListener(textWatcher);
        setToolbar(null);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            checkEmail(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    public void onViewClick(View view){
        LoadingDialog.getInstance(this).show();
        btn_send_email.setEnabled(false);
        String email = edt_email.getText().toString();
        presenter.doSendEmail(email);
    }

    private void checkEmail(String email){
        if (CommonUtils.chekEmail(email)){
            lb_email.setErrorEnabled(false);
            btn_send_email.setEnabled(true);
            return;
        }
        lb_email.setErrorEnabled(true);
        btn_send_email.setEnabled(false);
    }

    @Override
    public void onNotPerfect() {

    }

    @Override
    public void onHttpRequestComplete(boolean isSuccess) {

        if (isSuccess) {
            setResult(RESULT_OK);
            finish();
        }
        btn_send_email.setEnabled(true);
    }
}
